package com.pig4cloud.pig.module.controller.vip;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.vip.VipInfo;
import com.pig4cloud.pig.module.service.vip.IVipInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 会员价格Controller
 *
 * @author ruoyi
 * @date 2022-10-19
 */
@Validated
@Api(value = "会员价格控制器", tags = {"会员价格管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/vip/info")
public class VipInfoController {

    private final IVipInfoService vipInfoService;

    /**
     * 查询会员价格列表
     */
    @ApiOperation("查询会员价格列表")
    @PreAuthorize("@pms.hasPermission('module:vipInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<VipInfo> list(@Validated VipInfo vipInfo) {
        QueryWrapper<VipInfo> qw = new QueryWrapper<>();

        TableDataInfo<VipInfo> td = PageUtils.buildDataInfo(vipInfoService.page(PageUtils.buildPage(),qw));
        List<VipInfo> list = td.getData();
        return td;
    }

    /**
     * 导出会员价格列表
     */
    @ApiOperation("导出会员价格列表")
    @PreAuthorize("@pms.hasPermission('module:vipInfo:export')")
    @SysLog("会员价格")
    @GetMapping("/export")
    public List export(@Validated VipInfo vipInfo) {
        List<VipInfo> list = vipInfoService.list();
        // ExcelUtil<VipInfo> util = new // ExcelUtil<VipInfo>(VipInfo.class);
        return  list;// util.exportExcel(list, "会员价格");
    }

    /**
     * 获取会员价格详细信息
     */
    @ApiOperation("获取会员价格详细信息")
    @PreAuthorize("@pms.hasPermission('module:vipInfo:query')")
    @GetMapping("/{id}")
    public R<VipInfo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        VipInfo vipInfo = vipInfoService.getById(id);
        return R.ok(vipInfo);
    }

    /**
     * 新增会员价格
     */
    @ApiOperation("新增会员价格")
    @PreAuthorize("@pms.hasPermission('module:vipInfo:add')")
    @SysLog("会员价格")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody VipInfo vipInfo) {
        return R.okByRow(vipInfoService.save(vipInfo) ? 1 : 0);
    }

    /**
     * 修改会员价格
     */
    @ApiOperation("修改会员价格")
    @PreAuthorize("@pms.hasPermission('module:vipInfo:edit')")
    @SysLog("会员价格")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody VipInfo vipInfo) {
        return R.okByRow(vipInfoService.updateById(vipInfo) ? 1 : 0);
    }

    /**
     * 删除会员价格
     */
    @ApiOperation("删除会员价格")
    @PreAuthorize("@pms.hasPermission('module:vipInfo:remove')")
    @SysLog("会员价格")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(vipInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
